#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2021/10/19
# @Author  : 邓大大
# @Desc    : 追涨杀跌策略 ---fmz 平台

# 实现原理
"""当前价格上涨超过所设置的浮动率，就买入，当前下跌超过所设置的浮动率就卖出，卖出买入最低限制范围内"""

rate = 0.05
base_price = 0
my_lowwer = 0.01

def main():
    global rate, base_price
    ticket = _C(exchange.GetTicker())
    base_price = ticket.Last
    while True:
        ticket = _C(exchange.GetTicker())
        last_price = ticket.Last  # 最后的成交价
        if ticket.Last - base_price > 0 and (ticket.Last - base_price)/ base_price > rate:
            acc = _C(exchange.Account)
            if acc.Balance * rate /ticket.Last > my_lowwer:
                exchange.Buy(ticket.Last, acc.Balance*rate/ticket.Last)
                base_price = ticket.Last
        if ticket.Last - base_price < 0 and (base_price - ticket.Last)/ base_price > rate:
            # 买出
            acc = _C(exchange.Account)
            if acc.Stocks * rate > my_lowwer:
                exchange.Sell(ticket.Last, acc.Stocks * rate)
                base_price = ticket.Last

